import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Register extends StatefulWidget {
  _RegisterWidgetState createState() => new _RegisterWidgetState();
}

class _RegisterWidgetState extends State<Register> {
  TextEditingController _pwdcontroller = TextEditingController();
  TextEditingController _usernamecontroller = TextEditingController();
  TextEditingController _confirmcontroller = TextEditingController();
  FocusNode _focusNode_username = FocusNode();
  FocusNode _focusNode_password = FocusNode();
  FocusNode _focusNode_confirm = FocusNode();
  FocusNode _focusNode_code = FocusNode();
  var _backcolor_username = Colors.black;
  var _backcolor_password = Colors.black;
  var _backcolor_confirm = Colors.black;
  var _backcolor_code = Colors.black;
  GlobalKey _globalKey_username = new GlobalKey<FormState>();
  bool _isObscure_pwd = true;
  bool _isObscure_confirm = true;
  var _eyeColor_pwd = Colors.grey;
  var _eyeColor_confirm = Colors.grey;
  var _text_user = '';
  var _text_pwd = '';
  var _text_confirm = '';

  void initState() {
    //_globalKey_username.currentState.setState(() {});
    _focusNode_username.addListener(() {
      if (_focusNode_username.hasFocus) {
        setState(() {
          _backcolor_username = Colors.blue;
        });
      } else {
        setState(() {
          _backcolor_username = Colors.black;
        });
      }
    });
    _focusNode_password.addListener(() {
      if (_focusNode_password.hasFocus) {
        setState(() {
          _backcolor_password = Colors.blue;
        });
      } else {
        setState(() {
          _backcolor_password = Colors.black;
        });
      }
    });
    _focusNode_confirm.addListener(() {
      if (_focusNode_confirm.hasFocus) {
        setState(() {
          _backcolor_confirm = Colors.blue;
        });
      } else {
        setState(() {
          _backcolor_confirm = Colors.black;
        });
      }
    });
    _focusNode_code.addListener(() {
      if (_focusNode_code.hasFocus) {
        setState(() {
          _backcolor_code = Colors.blue;
        });
      } else {
        setState(() {
          _backcolor_code = Colors.black;
        });
      }
    });
  }

  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: Text('不知道起啥名好', textAlign: TextAlign.center),
          automaticallyImplyLeading: false,
        ),
        body: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.all(16.0),
                child: Text(
                  '注册',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 28.0,
                    height: 1.2,
                  ),
                ),
              ),
              Padding(
                  padding: EdgeInsets.only(bottom: 16.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.max,
                    children: <Widget>[
                      Text(
                        '请注册您的账号。',
                        style: TextStyle(
                            color: Colors.black, fontSize: 15.0, height: 1.0),
                      ),
                      Text(
                        '了解详情',
                        style: TextStyle(
                            color: Colors.blue, fontSize: 15.0, height: 1.0),
                      )
                    ],
                  )),
              Form(
                autovalidate: true,
                child: Column(
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.fromLTRB(16.0, 10.0, 16.0, 10.0),
                      child: Center(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Container(
                              decoration: BoxDecoration(
                                border: Border.all(
                                  width: 3.0,
                                  color: _backcolor_username,
                                ),
                                borderRadius: const BorderRadius.all(
                                    const Radius.circular(12.0)),
                              ),
                              child: TextField(
                                //key: _globalKey,
                                onChanged: (text) {
                                  if (text.trim().length == 0) {
                                    _text_user = '用户名不能为空';
                                    _backcolor_username = Colors.red;
                                  } else {
                                    _text_user = '';
                                    _backcolor_username = Colors.blue;
                                  }
                                },
                                controller: _usernamecontroller,
                                focusNode: _focusNode_username,
                                autofocus: false,
                                cursorColor: const Color(0xFFFF6B47),
                                cursorWidth: 1.0,
                                style: TextStyle(
                                  color: Color(0xFF4A4A4A),
                                  fontSize: 16.0,
                                ),
                                decoration: InputDecoration(
                                    border: InputBorder.none,
                                    fillColor: const Color(0xFFFF5F5F5),
                                    isDense: true,
                                    filled: true,
                                    labelText: "用户名",
                                    hintText: "请输入用户名",
                                    hintStyle: TextStyle(
                                      color: Color(0xFF9B9B9B),
                                      fontSize: 16.0,
                                    ),
                                    prefixIcon: Icon(Icons.person)),
                                /* validator: (v) {
                                    return v.trim().length > 0 ? null : "用户名不能为空";
                                  },*/
                              ),
                            ),
                            Padding(
                              padding: EdgeInsets.only(left: 16.0),
                              child: Text(
                                '$_text_user',
                                key: _globalKey_username,
                                style: TextStyle(
                                  fontSize: 12.0,
                                  color: Colors.red,
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.fromLTRB(16.0, 10.0, 16.0, 10.0),
                      child: Center(
                          child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Container(
                            decoration: BoxDecoration(
                              border: Border.all(
                                width: 3.0,
                                color: _backcolor_password,
                              ),
                              borderRadius: const BorderRadius.all(
                                  const Radius.circular(12.0)),
                            ),
                            child: TextField(
                              // key: _globalKey,
                              focusNode: _focusNode_password,
                              onChanged: (text) {
                                if (text.length < 5 || text.length >= 16) {
                                  setState(() {
                                    _text_pwd = '密码应为六到十五位字符';
                                    _backcolor_password = Colors.red;
                                  });
                                } else {
                                  setState(() {
                                    _text_pwd = '';
                                    _backcolor_password = Colors.blue;
                                    print(text);
                                  });
                                }
                              },
                              autofocus: false,
                              cursorColor: const Color(0xFFFF6B47),
                              cursorWidth: 1.0,
                              controller: _pwdcontroller,
                              style: TextStyle(
                                color: Color(0xFF4A4A4A),
                                fontSize: 16.0,
                              ),
                              obscureText: _isObscure_pwd,
                              decoration: InputDecoration(
                                  border: InputBorder.none,
                                  fillColor: const Color(0xFFFF5F5F5),
                                  isDense: true,
                                  filled: true,
                                  labelText: "密码",
                                  hintText: "请输入密码",
                                  hintStyle: TextStyle(
                                    color: Color(0xFF9B9B9B),
                                    fontSize: 16.0,
                                  ),
                                  suffixIcon: IconButton(
                                    icon: Icon(
                                      Icons.remove_red_eye,
                                      size: 25.0,
                                      color: _eyeColor_pwd,
                                    ),
                                    onPressed: () {
                                      setState(() {
                                        _isObscure_pwd = !_isObscure_pwd;
                                        _eyeColor_pwd = _isObscure_pwd
                                            ? Colors.grey
                                            : Colors.blue;
                                      });
                                    },
                                  ),
                                  prefixIcon: Icon(Icons.lock)),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 16.0),
                            child: Text(
                              '$_text_pwd',
                              style: TextStyle(
                                fontSize: 12.0,
                                color: Colors.red,
                              ),
                            ),
                          )
                        ],
                      )),
                    ),
                    Padding(
                      padding: EdgeInsets.fromLTRB(16.0, 10.0, 16.0, 10.0),
                      child: Center(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Container(
                              decoration: BoxDecoration(
                                border: Border.all(
                                  width: 3.0,
                                  color: _backcolor_confirm,
                                ),
                                borderRadius: const BorderRadius.all(
                                    const Radius.circular(12.0)),
                              ),
                              child: TextField(
                                // key: _globalKey,
                                controller: _confirmcontroller,

                                focusNode: _focusNode_confirm,
                                autofocus: false,
                                cursorColor: const Color(0xFFFF6B47),
                                cursorWidth: 1.0,
                                obscureText: _isObscure_confirm,
                                style: TextStyle(
                                  color: Color(0xFF4A4A4A),
                                  fontSize: 16.0,
                                ),
                                decoration: InputDecoration(
                                    suffixIcon: IconButton(
                                      icon: Icon(
                                        Icons.remove_red_eye,
                                        size: 25.0,
                                        color: _eyeColor_confirm,
                                      ),
                                      onPressed: () {
                                        setState(() {
                                          _isObscure_confirm =
                                              !_isObscure_confirm;
                                          _eyeColor_confirm = _isObscure_confirm
                                              ? Colors.grey
                                              : Colors.blue;
                                        });
                                      },
                                    ),
                                    border: InputBorder.none,
                                    fillColor: const Color(0xFFFF5F5F5),
                                    isDense: true,
                                    filled: true,
                                    labelText: "确认密码",
                                    hintText: "请确认密码",
                                    hintStyle: TextStyle(
                                      color: Color(0xFF9B9B9B),
                                      fontSize: 16.0,
                                    ),
                                    prefixIcon: Icon(Icons.lock_outline)),
                                /* validator: (v){
                           return v.trim().toString()==_pwdcontroller.text.toString()?null:"两次密码输入不一致";
                          },*/
                              ),
                            ),
                            Padding(
                              padding: EdgeInsets.only(left: 16.0),
                              child: Text(
                                '$_text_confirm',
                                style: TextStyle(
                                  fontSize: 12.0,
                                  color: Colors.red,
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.fromLTRB(16.0, 10.0, 16.0, 60.0),
                      child: Center(
                          child: Container(
                        decoration: BoxDecoration(
                          border: Border.all(
                            width: 3.0,
                            color: _backcolor_code,
                          ),
                          borderRadius: const BorderRadius.all(
                              const Radius.circular(12.0)),
                        ),
                        child: TextField(
                          enabled: false,
                          focusNode: _focusNode_code,
                          autofocus: false,
                          cursorColor: const Color(0xFFFF6B47),
                          cursorWidth: 1.0,
                          style: TextStyle(
                            color: Color(0xFF4A4A4A),
                            fontSize: 16.0,
                          ),
                          decoration: InputDecoration(
                              border: InputBorder.none,
                              fillColor: const Color(0xFFFF5F5F5),
                              isDense: true,
                              filled: true,
                              labelText: "验证码(暂时废弃)",
                              hintText: "请填写验证码",
                              hintStyle: TextStyle(
                                color: Color(0xFF9B9B9B),
                                fontSize: 16.0,
                              ),
                              prefixIcon: Icon(Icons.verified_user)),
                        ),
                      )),
                    ),
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(bottom: 60.0),
                child: Wrap(
                  spacing: 85.0,
                  children: <Widget>[
                    FlatButton(
                      color: Color(0x000001),
                      colorBrightness: Brightness.dark,
                      textColor: Colors.blue,
                      child: Text('返回'),
                      //textColor: Colors.white,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20.0)),
                      onPressed: () {
                        Navigator.popAndPushNamed(context, 'First');
                      },
                    ),
                    RaisedButton(
                      color: Colors.blue,
                      highlightColor: Colors.blue[700],
                      colorBrightness: Brightness.dark,
                      child: Text('下一步'),
                      //textColor: Colors.white,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20.0)),
                      onPressed: () {
                        bool flag = true;
                        if (_usernamecontroller.text.trim().length == 0) {
                         setState(() {
                           _backcolor_username = Colors.red;
                           _text_user = "用户名不能为空";
                         });
                        } else {
                          setState(() {
                            _backcolor_username = Colors.black;
                            _text_user = "";
                          });
                          if (_pwdcontroller.text.trim().length > 5 &&
                              _pwdcontroller.text.trim().length < 16) {
                            setState(() {
                              _backcolor_password=Colors.black;
                              _text_pwd="";
                            });
                            if (_pwdcontroller.text.trim() ==
                                _confirmcontroller.text.trim()) {
                              Navigator.pushNamed(context, "person_data");
                              setState(() {
                                _backcolor_confirm = Colors.black;
                                _text_confirm = '';
                              });
                            } else {
                              setState(() {
                                _backcolor_confirm = Colors.red;
                                _text_confirm = '两次密码输入不一致';
                              });
                            }
                          } else {
                           setState(() {
                             _backcolor_password=Colors.red;
                             _text_pwd="密码应为六到十五位字符";
                           });
                          }
                        }
                      },
                    )
                  ],
                ),
              )
            ],
          ),
        ));
  }
}
